package com.anan.controller;

import com.anan.constants.Constants;
import com.anan.pojo.dto.*;
import com.anan.pojo.vo.OrderVO;
import com.anan.pojo.vo.ProductInfosVO;
import com.anan.pojo.vo.ProductVO;
import com.anan.service.ProductService;
import com.anan.utils.ComResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author anan
 * @date 2023/05/06/10:44
 **/
@Slf4j
@RestController
@RequestMapping("/salvage/products")
public class ProductController {

    @Resource
    private ProductService productService;


    @PostMapping("/addProduct")
    public ComResult addProduct(@RequestBody ProductDTO productDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if (null != productDTO) {
            if (null == productDTO.getStudentId()) {
                log.debug("未传学生id");
                return comResult;
            }

            if (null == productDTO.getProductType() || productDTO.getProductType() <= 0) {
                comResult.setMessage("请补全所有商品的商品类别");
            }
            if (null == productDTO.getStatus() || productDTO.getStatus() <= 0) {
                comResult.setMessage("请补全所有商品的商品状态");
            }
            if (null == productDTO.getPrice() || productDTO.getPrice() <= 0) {
                comResult.setMessage("请补全所有商品的商品价格");
            }
            if (StringUtils.isBlank(productDTO.getTitle())) {
                comResult.setMessage("请补全所有商品的商品标题");
            }

            boolean flag = productService.addProducts(productDTO);

            if (flag) {
                comResult.setCode(Constants.RET_CODE.SUCCESS);
                comResult.setMessage("上传成功！");
            } else {
                comResult.setMessage("上传失败");
            }

        }
        return comResult;
    }


    /**
     * 获取商品信息
     */
    @PostMapping("/getProductInfos")
    public ComResult getProductInfos(@RequestBody StatusDTO statusDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);
        ProductInfosVO productInfosVO = productService.getAllProductInfos(statusDTO.getAdminStatus(), statusDTO.getProductStatus());

        if (null != productInfosVO) {
            comResult.setData(productInfosVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
        }
        return comResult;
    }

    /**
     * 购买商品并返回订单信息
     */
    @RequestMapping("/buyProduct")
    public ComResult buyProduct(@RequestBody BuyProductDTO buyProductDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if(null == buyProductDTO.getProductId()) {
            log.info("===== 未传入商品id =====");
            comResult.setMessage("未传入商品id");
            return comResult;
        }

        if(null == buyProductDTO.getStudentId()) {
            log.info("===== 未传入用户id =====");
            comResult.setMessage("未传入用户id");
            return comResult;
        }

        OrderVO orderVO = productService.buyProduct(buyProductDTO);

        if (null != orderVO) {
            comResult.setData(orderVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("购买成功！");
        } else {
            comResult.setMessage("购买失败");
        }
        return comResult;
    }

    /**
     * 模糊搜索商品
     */
    @PostMapping("/getProductByLike")
    public ComResult getProductByLike(@RequestBody ProductLikeDTO productLikeDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if(null == productLikeDTO.getQryName()) {
            log.info("===== 请输入查询条件 =====");
            comResult.setMessage("请输入查询条件");
            return comResult;
        }

        ProductInfosVO productInfosVO = productService.getProductByLike(productLikeDTO.getQryName());

        if (null != productInfosVO) {
            comResult.setData(productInfosVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("购买成功！");
        } else {
            comResult.setMessage("购买失败");
        }

        return comResult;
    }

    /**
     * 商品上下架
     */
    @PostMapping("/updateStatus")
    public ComResult updateStatus(@RequestBody UpdateStatusDTO updateStatusDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if(null == updateStatusDTO.getStatus() || updateStatusDTO.getStatus() < 0) {
            comResult.setMessage("商品状态未传！");
            return comResult;
        }

        if(null == updateStatusDTO.getProductId() || updateStatusDTO.getProductId() < 0) {
            comResult.setMessage("商品id未传！");
            return comResult;
        }

        boolean flag = productService.updateShelvesStatus(updateStatusDTO);
        if(flag) {
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            if(updateStatusDTO.getStatus() == 1) comResult.setMessage("上架成功！");
            if(updateStatusDTO.getStatus() == 2) comResult.setMessage("下架成功！");
        } else {
            comResult.setMessage("上架失败");
        }
        return comResult;
    }
}
